package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.TextAttribute;
import java.text.AttributedString;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import main.Conference;
import main.Manuscript;
import main.Review;
import main.Status;
/**
 * pc makes decision frame.
 * @author Amandeep Jhajj
 * Cristopher updated for business rule #10
 * 
 * @version 12/4
 */
public class PCDecisionFrame {
	//GUI 
	JFrame frame= new JFrame("ManuView -- Paper Approval");
	JPanel my_top_panel = new JPanel();
	JPanel my_manu_panel = new JPanel();
	JPanel my_review_panel = new JPanel();
	JPanel my_button_panel = new JPanel();
	//Data
	private Manuscript my_manu;
	private Status my_initial_status;
	private Conference my_conf; 
		
	/**
	 * bring stuff over from the pc view frame.
	 * @param manu the manuscripts
	 * @param conf the conference
	 */
	public PCDecisionFrame(Manuscript manu, Conference conf) {
			my_manu = manu;
			my_conf = conf;
			my_initial_status = manu.getStatus();
		}
	/**
	 * starts the gui
	 */
	public void start(){
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Border blackborder = BorderFactory.createLineBorder(Color.BLACK);
		my_manu_panel.setBorder(blackborder);// unsubmit?
		my_review_panel.setBorder(blackborder);// Where the titles are
		my_button_panel.setBorder(blackborder);// bottom upload area
		my_top_panel.setBorder(blackborder);
		frame.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		//adding the 4 panels to our frame
		//manuscript panel
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets(1,2,1,2);
	    c.gridx = 0;
	    c.weightx = 1;
	    c.weighty = 5;
	    c.gridy = 1;
	    frame.add(my_manu_panel, c);
	    //review panel
	    c.fill = GridBagConstraints.BOTH;
	    c.insets = new Insets(1,1,2,2);
	    c.gridx = 1;
	    c.weightx = 1;
	    c.weighty = 5;
	    c.gridy = 1;
		frame.add(my_review_panel, c);
		//the bottom panels for buttons
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets(2,2,2,1);
		c.gridwidth = 2;
		c.weightx = 2;
		c.weighty = 1;
	    c.gridx = 0;
	    c.gridy = 2;
	    frame.add(my_button_panel, c);
	    //the top panel for title and author
	    c.fill = GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 2;
		c.gridwidth =2;
		c.weighty = .25;
		frame.add(my_top_panel, c);
	    
		
		String menu = "MANUSCRIPT:" + "\n";
		menu += my_manu.getText();
		
		//a test to make sure the scroll was working
		//for(int i = 0; i < 10000; i++)
			//menu += "-";
		//text area for the manuscript
	    JTextArea textPane = new JTextArea(menu);
	    textPane.setLineWrap(true);
	    my_manu_panel.setLayout(new BorderLayout());
	    JScrollPane scroll1 = new JScrollPane(textPane);
		scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroll1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		my_manu_panel.add(scroll1, BorderLayout.CENTER);
		
		String rev = "REVIEWS:" + "\n";
		//checked test to check whats in the revs..
		//int revs = my_manu.getReviews().size();
		//String aString = Integer.toString(revs);
		//test...
		/*for (int x = 0; x <= my_manu.getReviews().size(); x++){
			Review review = my_manu.getReviews().get(x);
			rev += review.getText();
			rev += "\n" + "Review" + i + "\n";
			i++;
		}*/
		
		//gets the text for reviews
		int i = 1;	
		for(Review element: my_manu.getReviews()){
			rev += "Review" + i +")\t" + element.getText() + "\n-" + element.getAuthor() + "\n\n";
			i++;
		}
		//text are for the review
		JTextArea textPane2 = new JTextArea(rev);
		textPane2.setLineWrap(true);
		my_review_panel.setLayout(new BorderLayout());
		JScrollPane scroll2 = new JScrollPane(textPane2);
		scroll2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroll2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		my_review_panel.add(scroll2, BorderLayout.CENTER);
		
		// the bottom buttons
		my_button_panel.setLayout(new GridLayout(1,4));
		final JButton approveButton = new JButton("Approve");
		approveButton.setBackground(Color.WHITE);
		final JButton rejectButton = new JButton("Reject");
		rejectButton.setBackground(Color.WHITE);
		final JButton undecidedButton = new JButton("Undecided");
		undecidedButton.setBackground(Color.WHITE);
		final JButton submitButton = new JButton("Back");
		submitButton.setBackground(Color.LIGHT_GRAY);
		//adding the buttons
		my_button_panel.add(approveButton);
		my_button_panel.add(rejectButton);
		my_button_panel.add(undecidedButton);
		my_button_panel.add(submitButton);
		
		Border black = new LineBorder(Color.BLACK, 3);
		Border green = new LineBorder(Color.GREEN, 3);
		Border red = new LineBorder(Color.RED, 3);
		Border yellow = new LineBorder(Color.YELLOW, 3);
		//set borders of buttons based on inital status of the manuscript.
		Border assignedBorder = black;
		if (my_initial_status == Status.APPROVED){
			assignedBorder = green;
		}
		approveButton.setBorder(assignedBorder);
		if (my_initial_status == Status.REJECTED) {
			assignedBorder = red;
		}else {
			assignedBorder = black;
		}
		rejectButton.setBorder(assignedBorder);
		if (my_initial_status == Status.UNDECIDED) {
			assignedBorder = yellow;
		}else {
			assignedBorder = black;
		}
		undecidedButton.setBorder(assignedBorder);
		submitButton.setBorder(black);
		//action listener for approve button
		approveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//perform the following action if no other options have been selected
				//and approved is not the "initial" status of the manuscript upon entering
				//the screen. OR perform the following action if other options have
				//been selected.
				if (((my_initial_status == my_manu.getStatus()) && 
						(my_initial_status != Status.APPROVED)) ||
						my_initial_status != my_manu.getStatus()) {
					Border green = new LineBorder(Color.GREEN, 3);
					approveButton.setOpaque(true);
					approveButton.setBorder(green);
					Border black = new LineBorder(Color.BLACK, 3);
					rejectButton.setBorder(black);
					undecidedButton.setBorder(black);
					//handle when not all reviews are in
					if (my_manu.getReviewers().size() > my_manu.getNumReviews()) {
						int cont = JOptionPane.showConfirmDialog(null, 
								"There are outstanding reviews for this manuscript. \n" +
								"Do you want to continue?", "Confirm",
						        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
						if (cont == JOptionPane.YES_OPTION) {
							my_manu.setStatus(Status.APPROVED);
							submitButton.setText("Submit");
						} else { //"de-select" the approved button
							approveButton.setBorder(black);
						}
					} else {
						my_manu.setStatus(Status.APPROVED);
						submitButton.setText("Submit");
					}
				}
			}
			});
		//action listener for reject button
		rejectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//perform the following action if no other options have been selected
				//and rejected is not the "initial" status of the manuscript upon entering
				//the screen. OR perform the following action if other options have
				//been selected.
				if (((my_initial_status == my_manu.getStatus()) && 
						(my_initial_status != Status.REJECTED)) ||
						my_initial_status != my_manu.getStatus()) {
					Border red = new LineBorder(Color.RED, 3);
					rejectButton.setOpaque(true);
					rejectButton.setBorder(red);
					Border black = new LineBorder(Color.BLACK, 3);
					approveButton.setBorder(black);
					undecidedButton.setBorder(black);
					//handle when not all reviews are in
					if (my_manu.getReviewers().size() > my_manu.getNumReviews()) {
						int cont = JOptionPane.showConfirmDialog(null, 
								"There are outstanding reviews for this manuscript. \n" +
								"Do you want to continue?", "Confirm",
						        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
						if (cont == JOptionPane.YES_OPTION) {
							my_manu.setStatus(Status.REJECTED);
							submitButton.setText("Submit");
						} else {
							rejectButton.setBorder(black);
						}
					} else {
						my_manu.setStatus(Status.REJECTED);
						submitButton.setText("Submit");
					}
				}
			}
			});
		//action listener for undecided button
		undecidedButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//perform the following action if no other options have been selected
				//and undecided is not the "initial" status of the manuscript upon entering
				//the screen. OR perform the following action if other options have
				//been selected.
				if (((my_initial_status == my_manu.getStatus()) && 
					(my_initial_status != Status.UNDECIDED)) ||
					my_initial_status != my_manu.getStatus()) {
					Border yellow = new LineBorder(Color.YELLOW, 3);
					undecidedButton.setOpaque(true);
					undecidedButton.setBorder(yellow);
					Border black = new LineBorder(Color.BLACK, 3);
					approveButton.setBorder(black);
					rejectButton.setBorder(black);
					my_manu.setStatus(Status.UNDECIDED);
					submitButton.setText("Submit");
				}
			}
			});
		//action listener for submit button
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_conf.save();
				PCViewsFrame vf = new PCViewsFrame(my_conf);
				vf.start();
				frame.dispose();
			}
			});
		my_top_panel.setLayout(new GridLayout(1,5));
		String myAuthor = my_manu.getAuthor();
		my_top_panel.add(new JLabel("Author:"));
		my_top_panel.add(new JLabel(myAuthor));
		String myTitle = my_manu.getTitle();
		my_top_panel.add(new JLabel("Title:"));
		my_top_panel.add(new JLabel(myTitle));
		frame.pack();
		frame.setSize(900,650);
		frame.setVisible(true);
	}
}
